import { Context, Next } from "koa";
import * as Vindex from '/App/validate/index';


export default function isLogin(filterApi: Array<string>) {

    return async (ctx: Context, next: Next) => {
        if (!filterApi.includes(ctx.reqPath.apiName)) {
            const data = ctx.request.headers; // 获取请求头
            // 开始验证token
            try {
                Vindex.Validate('token').check(data);
            } catch (error) {
                throw [7501, (error as Error).message];
            }
            // 字段验证通过（开始验证token）
            const result = ctx.tokenVerify(String(data.authorization).split(' ')[1]);
            if (!result) throw [7500, 'token过期或者无效'];
            ctx.__data = result;
        }

        await next();
    }
}

